package com.shiroexploit.util;

import java.io.*;
import java.util.*;

public class ConfigReader {

    public static List<String> loadKeys(int flag){
        //Set用于去重，使用 List 作为返回结果的目的是保留 key 从配置文件中读取的顺序
        List<String> keys = new ArrayList<>();
        Set<String> sets = new HashSet<>();

        String path;
        if(flag == 0){
            path = System.getProperty("user.dir") + File.separator + "config" + File.separator + "keys.conf";
        }else{
            path = System.getProperty("user.dir") + File.separator + "config" + File.separator + "keys.conf.big";
        }

        try {
            FileInputStream inputStream = new FileInputStream(path);
            BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
            String temp = "";
            while((temp = br.readLine()) != null){
                if(sets.add(temp))  keys.add(temp);
            }
        } catch (FileNotFoundException e) {
            //doNothing
        } catch (IOException e) {
            //doNothing
        }

        return keys;
    }

    public static String getConfig(String key){
        String path = System.getProperty("user.dir") + File.separator + "config" + File.separator + "config.properties";

        Properties properties = new Properties();
        FileInputStream in = null;
        String value = null;
        try {
            in = new FileInputStream(path);
            properties.load(in);
            value = properties.getProperty(key);
            in.close();
        } catch (FileNotFoundException e) {
            //doNothing
        } catch (IOException e) {
            //doNothing
        }

        return value;
    }
}
